﻿using Albatross.WPF.Common;
using Albatross.WPF.Common.PubSub;
using Albatross.WPF.Common.ViewModels;
using Albatross.WPF.Common.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Albatross.Shell {
	/// <summary>
	/// Interaction logic for DialogWindow.xaml
	/// </summary>
	public partial class DialogWindow : Window , IViewService{
		public DialogWindow() {
			InitializeComponent();
			Publisher = new Publisher();
			SetValue(Attached.ViewServiceProperty, this);
			this.Closing += DialogWindow_Closing;
		}

		void DialogWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
			try {
				e.Cancel = !ViewServiceExt.TryCloseView(this);
			} catch (Exception err) {
				ShowErrorMessageBox(err.Message);
				e.Cancel = true;
			}
		}

		public WPF.Common.PubSub.IPublisher Publisher { get; private set; }

		public void LoadElement(UIElement elem) { }
		public void UnloadElement(UIElement elem) { }
		public bool? ShowDialog(IWorkspaceConstruct construct, int? width, int? height) {
			return ViewServiceExt.ShowDialog<DialogWindow>(this, construct, width, height);
		}

		public void ShowWorkspace(IWorkspaceConstruct construct) {
			try {
				if (ViewServiceExt.TryCloseView(this)) {
					Content = construct.GetView();
					DataContext = construct.Build();
					SetValue(Attached.WorkspaceConstructProperty, construct);
					if (construct.Constructed != null) { construct.Constructed.AsyncLoadHandler(this); }
				}
			} catch (Exception err) {
				ShowErrorMessageBox(err.Message);
			}
		}

		public bool CloseWorkspace(IWorkspaceViewModel vm) {
			if (vm == DataContext) {
				try {
					if (ViewServiceExt.TryCloseView(this)) {
						DialogResult = true;
						return true;
					}
				} catch (Exception err) {
					ShowErrorMessageBox(err.Message);
				}
			}
			return false;
		}

		public void CreateWindow(IWorkspaceConstruct construct) { throw new NotSupportedException(); }

		public void ShowWarningMessageBox(string msg) {
			IInputElement elem = Keyboard.FocusedElement;
			MessageBox.Show(this, msg, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
			if (elem != null) { Keyboard.Focus(elem); }
		}

		public void ShowErrorMessageBox(string msg) {
			IInputElement elem = Keyboard.FocusedElement;
			MessageBox.Show(this, msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
			if (elem != null) { Keyboard.Focus(elem); }
		}

		public void ShowInfoMessageBox(string msg) {
			IInputElement elem = Keyboard.FocusedElement;
			MessageBox.Show(this, msg, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
			if (elem != null) { Keyboard.Focus(elem); }
		}

		public bool ShowConfirmationMessageBox(string msg) {
			IInputElement elem = Keyboard.FocusedElement;
			bool value = MessageBox.Show(this, msg, "Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK;
			if (elem != null) { Keyboard.Focus(elem); }
			return value;
		}
	}
}
